<?php

namespace StarCloud\Library\Alipay;


class Cashwith{

    public function withdr($orderid,$userid='',$money='',$remarks='',$user='',$payee = ''){
        try{
            $pei=\think\facade\Config::load('setting/alipay','alipay');
            $aop = new AopCertClient();
            $aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do'; //请求url
            $aop->appId = $pei['appid'];        //商家支付宝APPID
            $aop->rsaPrivateKey = $pei['alimach'];    //私钥
            $aop->alipayrsaPublicKey=$pei['alikey'];    //支付宝公钥
            $aop->apiVersion = '1.0';    //版本号
            $aop->signType = 'RSA2';    //加密方式
            $aop->postCharset='utf-8';
            $aop->format='xml';  //支付宝返回方式
            $out_biz_no = $orderid;    //订单号

            if($payee == 'BANKCARD') {
                $request = new AlipayFundTransUniTransferRequest();
                $request->setBizContent("{" .
                    "\"out_biz_no\":\"".$out_biz_no."\",".
                	"\"remark\":\"".$remarks."\",".
                	"\"biz_scene\":\"DIRECT_TRANSFER\",".
                	"\"payee_info\":{".
                		"\"identity\":\"".$userid."\",".
                		"\"identity_type\":\"BANKCARD_ACCOUNT\",".
                		"\"name\":\"".$user."\"".
                	"},".
                	"\"trans_amount\":\"".$money."\",".
                	"\"product_code\":\"TRANS_ACCOUNT_NO_PWD\",".
                	"\"order_title\":\"".$remarks."\"".
                    "  }");       

            } else {
                $request = new AlipayFundTransToaccountTransferRequest();
                $request->setBizContent("{" .
                    "\"out_biz_no\":\"".$out_biz_no."\"," .
                    "\"payee_type\":\"".$payee."\"," .
                    "\"payee_account\":\"".$userid."\"," .
                    "\"amount\":\"".$money."\"," .
                    "\"payer_show_name\":\"".$pei['bei']."\"," .
                    "\"payee_real_name\":\"".$user."\"," .
                    "\"remark\":\"".$remarks."\"" .
                    "  }");       
            }

            $result = $aop->execute ( $request);
            $result = json_decode(json_encode($result),true);
            if($result['code']== 10000 && $result['msg']=='Success'){
                $data['code'] = 1;
                $data['msg'] = '提现成功';
                $data['out_biz_no'] = $out_biz_no;
                return  $data;
            }else{
                $data['code'] = 0;
                $data['msg'] = $result['sub_msg'];
                return $data;
            }
        }catch (\Exception $e) {
            $data['code'] = 0;
            $data['msg'] = $e->getMessage();
            return $data;
        }
    }

}